package com.ruoyi.crm.controller.business;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.crm.domain.vo.BusinessVO;
import com.ruoyi.crm.domain.vo.ResourceGainVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.crm.domain.Business;
import com.ruoyi.crm.service.IBusinessService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商机管理Controller
 *
 * @author ruoyi
 * @date 2022-06-07
 */
@RestController
@RequestMapping("/business")
public class BusinessController extends BaseController
{
    @Autowired
    private IBusinessService businessService;

    /**
     * 查询商机管理列表
     */
    @PreAuthorize("@ss.hasPermi('crm:business:list')")
    @GetMapping("/list")
    public TableDataInfo list(Business business)
    {
        startPage();
        List<BusinessVO> list = businessService.selectBusinessVOList(business);
        return getDataTable(list);
    }

    /**
     * 公海池
     */
    @GetMapping("/pool")
    public TableDataInfo pool(Business business){
        startPage();
        List<BusinessVO> businessVOS = businessService.selectBusinessPoolList(business);
        return getDataTable(businessVOS);
    }

    /**
     * 导出商机管理列表
     */
    @PreAuthorize("@ss.hasPermi('crm:business:export')")
    @Log(title = "商机管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Business business)
    {
        List<Business> list = businessService.selectBusinessList(business);
        ExcelUtil<Business> util = new ExcelUtil<Business>(Business.class);
        util.exportExcel(response, list, "商机管理数据");
    }

    /**
     * 获取商机管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('crm:business:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(businessService.selectBusinessById(id));
    }

    /**
     * 新增商机管理
     */
    @PreAuthorize("@ss.hasPermi('crm:business:add')")
    @Log(title = "商机管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Business business)
    {
        return toAjax(businessService.insertBusiness(business));
    }

    /**
     * 修改商机管理
     */
    @PreAuthorize("@ss.hasPermi('crm:business:edit')")
    @Log(title = "商机管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Business business)
    {
        return toAjax(businessService.updateBusiness(business));
    }

    /**
     * 删除商机管理
     */
    @PreAuthorize("@ss.hasPermi('crm:business:remove')")
    @Log(title = "商机管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(businessService.deleteBusinessByIds(ids));
    }

    /**
     * 踢回公海
     * @param busId
     * @param busReasonsId
     * @return
     */
    @PutMapping("/back/{busId}/{busReasonsId}")
    public AjaxResult backBusiness(@PathVariable String busId,
                                  @PathVariable String busReasonsId){
        return businessService.backBusiness(busId,busReasonsId);
    }

    /**
     * 捞取
     */
    @PutMapping("/gain")
    public AjaxResult businessGain(@RequestBody ResourceGainVO resourceGainVO){
        return businessService.businessGain(resourceGainVO);
    }

    /**
     * 分配
     * @param resourceGainVO
     * @return
     */
    @PutMapping("/assignment")
    public AjaxResult assignment(@RequestBody ResourceGainVO resourceGainVO){
        return businessService.assignment(resourceGainVO);
    }
}
